package guda.talk.biz.common.auth;

import com.alibaba.citrus.util.Assert;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;


import static com.alibaba.citrus.util.Assert.assertTrue;
import static com.alibaba.citrus.util.BasicConstant.EMPTY_STRING_ARRAY;
import static com.alibaba.citrus.util.ObjectUtil.defaultIfNull;
import guda.talk.biz.common.enums.RoleEnum;

/**
 * Created by well on 15/3/16.
 */
public class AuthUser implements java.io.Serializable {

    private static final ThreadLocal<AuthUser> authHolder = new ThreadLocal<AuthUser>();

    private Long userId;
    private String[] roles;
    private String loginName;
    private String phone;

    private String companyName;
    private boolean firstLogin;

    public static final AuthUser getCurrentUser() {
        return authHolder.get();
    }

    public static final void setCurrentUser(AuthUser buyer) {
        authHolder.set(buyer);
    }

    public static final void cleanThreadLocal() {
        authHolder.remove();
    }

    /**
     * 创建匿名用户。
     */
    public AuthUser() {

    }

    public boolean isAdmin(){
        if(roles == null || roles.length == 0){
            return false;
        }
        for(String s:roles) {
            if (RoleEnum.admin.name().equals(s)){
                return true;
            }
        }
        return false;
    }

    public boolean isUser(){
        if(roles == null || roles.length == 0){
            return false;
        }
        for(String s:roles) {
            if (RoleEnum.user.name().equals(s)){
                return true;
            }
        }
        return false;
    }

    /**
     * 创建用户。
     */
    public AuthUser(Long adminId) {
        this.userId = adminId;
    }


    public Long getUserId() {
        return userId;
    }

    public String[] getRoles() {
        return defaultIfNull(roles, EMPTY_STRING_ARRAY);
    }

    public void setRoles(String[] roles) {
        this.roles = roles;
    }

    public void upgrade(Long adminId, String[] roles) {
        assertTrue(!hasLoggedIn(), Assert.ExceptionType.ILLEGAL_STATE, "already logged in");
        this.userId = adminId;
        this.roles = roles;
        //setCurrentUser(this);
    }

    public boolean hasLoggedIn() {
        return userId != null;
    }


    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public boolean getFirstLogin() {
        return firstLogin;
    }

    public void setFirstLogin(Boolean firstLogin) {
        this.firstLogin = firstLogin;
    }
}
